package com.kh.first;

import com.kh.first.pojo.ProductReportPojo;
import com.kh.first.pojo.ReportDetailPojo;
import com.kh.first.pojo.ReportNotesPojo;
import com.kh.first.utils.Len;
import com.kh.first.utils.TN;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@TN("fff")
public class GenTable {
    Runnable runnable = () -> {
        TN annotation = GenTable.class.getAnnotation(TN.class);
        String value = annotation.value();
        System.out.println(value);
    };

    public static void main(String[] args) {
        genTable(ReportNotesPojo.class);
    }

    private static String genTable(Class<?> clazz) {
        String tableName = clazz.getAnnotation(TN.class).value();
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("drop table if exists %s;\n", tableName));
        sb.append(String.format("create table `%s` (\n", tableName));
        sb.append("id bigint(20) auto_increment,\n");
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
            Len len = field.getAnnotation(Len.class);
            Class<?> clazzInt = Integer.class;
            Class<?> clazzDate = Date.class;
            Class<?> clazzString = String.class;
            Class<?> clazzLong = Long.class;
            Class<?> clazzArray = List.class;
            Class<?> clazzBigDecimal = BigDecimal.class;
            int fLength = 0;// 字符串的长度
            String sqlFieldType = "";
            if (null != len) {// 有长度的字符串和整数怎么办？
                fLength = len.value();
            } else {
                if (type == clazzInt) {
                    fLength = 3;
//					sqlFieldType=" int("
                }
            }
            if (type == clazzInt) {
                fLength = 2;
                sb.append(name + " int(3)");
            } else if (type == clazzString) {
                sb.append(name + " varchar(255)");
            } else if (type == clazzDate) {
                sb.append(name + " datetime");
            } else if (type == clazzBigDecimal) {
                sb.append(name + " decimal(4,2)");
            } else if (type == clazzLong) {
                sb.append(name + " bigint(20)");
            } else if (type == clazzArray) {
                sb.append(name + " varchar(255)");
            }
            sb.append(",\n");
        }

        sb.append("primary key (`id`)\n" + ")");
        String string = sb.toString();
        System.out.println(string);
        return string;
    }
}
